package com.lpw.dbvideo.video.provider;

import com.lpw.dbvideo.video.domain.Video;
import org.apache.ibatis.jdbc.SQL;

/**
 * video 构建动态sql语句
 */
public class VideoProvider {

    /**
     * 更新video动态语句
     *
     * @param video
     * @return
     */
    public String updateVideo(final Video video) {
        return new SQL() {
            {
                UPDATE("video");

                // 条件写法
                if (video.getTitle() != null) {
                    SET("title=#{title}");
                }
                if (video.getSummary() != null) {
                    SET("summary=#{summary}");
                }
                if (video.getCoverImg() != null) {
                    SET("cover_img=#{coverImg}");
                }
                if (video.getViewNum() != null) {
                    SET("view_num=#{viewNum}");
                }
                if (video.getPrice() != null) {
                    SET("price=#{price}");
                }
                if (video.getCreateTime() != null) {
                    SET("create_time=#{createTime}");
                }
                if (video.getOnline() != null) {
                    SET("online=#{online}");
                }
                if (video.getPoint() != null) {
                    SET("point=#{point}");
                }
                WHERE("id=#{id}");
            }
        }.toString();
    }

    /**
     * 新增video动态sql语句
     * @param video 新郑的video对象
     * @return
     */
    public String insertVideo(final Video video){
        return new SQL(){
            {
                INSERT_INTO("video");
                if(video.getTitle() != null){
                    VALUES("title","#{title}");
                }
                if(video.getSummary() != null){
                    VALUES("summary","#{summary}");
                }
                if(video.getCoverImg() != null){
                    VALUES("cover_img","#{coverImg}");
                }
                if(video.getViewNum() != null){
                    VALUES("view_num","#{viewNum}");
                }
                if(video.getPrice() != null){
                    VALUES("price","#{price}");
                }
                if(video.getCreateTime() != null){
                    VALUES("create_time","#{createTime}");
                }
                if(video.getOnline() != null){
                    VALUES("online","#{online}");
                }
                if(video.getPoint() != null){
                    VALUES("point","#{point}");
                }
            }
        }.toString();
    }

}
